from PyQt6.QtWidgets import (QWidget, QVBoxLayout, QToolButton, 
                           QSizePolicy)
from PyQt6.QtCore import Qt

class CollapsibleBox(QWidget):
    """可折叠的容器组件"""
    def __init__(self, title="", parent=None, expanded=True):
        super().__init__(parent)
        
        self.toggle_button = QToolButton()
        self.toggle_button.setStyleSheet("""
            QToolButton { 
                border: none;
                padding: 10px;
                font-size: 14px;
            }
            QToolButton:hover {
                background-color: #e0e0e0;
            }
        """)
        self.toggle_button.setToolButtonStyle(Qt.ToolButtonStyle.ToolButtonTextBesideIcon)
        self.toggle_button.setArrowType(Qt.ArrowType.DownArrow if expanded else Qt.ArrowType.RightArrow)
        self.toggle_button.setText(title)
        self.toggle_button.setCheckable(True)
        self.toggle_button.setChecked(expanded)
        
        self.content_area = QWidget()
        self.content_area.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Fixed)
        
        self.content_layout = QVBoxLayout()
        self.content_area.setLayout(self.content_layout)
        
        lay = QVBoxLayout(self)
        lay.setSpacing(0)
        lay.setContentsMargins(0, 0, 0, 0)
        lay.addWidget(self.toggle_button)
        lay.addWidget(self.content_area)
        
        self.toggle_button.clicked.connect(self.on_toggle)
        
        # 设置初始状态
        if expanded:
            self.content_area.setMaximumHeight(16777215)  # 设置一个很大的值
        else:
            self.content_area.setMaximumHeight(0)
        
    def on_toggle(self, checked):
        self.toggle_button.setArrowType(
            Qt.ArrowType.DownArrow if checked else Qt.ArrowType.RightArrow
        )
        self.content_area.setMaximumHeight(16777215 if checked else 0)
    
    def addWidget(self, widget):
        self.content_layout.addWidget(widget)
        
    def addLayout(self, layout):
        self.content_layout.addLayout(layout) 